package no.uib.prideconverter.gui;

import no.uib.prideconverter.PRIDEConverter;
import java.awt.Dimension;
import java.awt.Point;
import java.awt.Toolkit;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import javax.swing.JFileChooser;
import javax.swing.JFormattedTextField;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import no.uib.prideconverter.filefilters.PepXmlFileFilter;
import no.uib.prideconverter.filefilters.ProtXmlFileFilter;
import uk.ac.ebi.pride.model.interfaces.mzdata.CvParam;
import uk.ac.ebi.pride.model.interfaces.mzdata.UserParam;

/**
 * This frame handles the selection of the data files to be converted for 
 * TPP data sets (Peptide-/ProteinProphet).
 * 
 * @author Harald Barsnes
 * 
 * Created November 2008
 */
public class DataFileSelectionTPP extends javax.swing.JFrame {


    /** 
     * Opens a new DataFileSelectionTPP frame, and inserts stored information.
     * 
     * @param location where to position the frame
     */
    public DataFileSelectionTPP(Point location) {

        // sets the default wizard frame size
        this.setPreferredSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setSize(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT);
        this.setMaximumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));
        this.setMinimumSize(new Dimension(PRIDEConverter.getProperties().FRAME_WIDTH,
                PRIDEConverter.getProperties().FRAME_HEIGHT));

        initComponents();

        JFormattedTextField tf = ((JSpinner.DefaultEditor) peptideProphetConfidenceLevelJSpinner.getEditor()).getTextField();
        tf.setHorizontalAlignment(JFormattedTextField.CENTER);

        tf = ((JSpinner.DefaultEditor) proteinProphetConfidenceLevelJSpinner.getEditor()).getTextField();
        tf.setHorizontalAlignment(JFormattedTextField.CENTER);

        // insert stored information
        if (PRIDEConverter.getProperties().getPeptideProphetFileName() != null) {
            peptideProphetFileJTextField.setText(PRIDEConverter.getProperties().getPeptideProphetFileName());
        }

        if (PRIDEConverter.getProperties().getProteinProphetFileName() != null) {
            proteinProphetFileJTextField.setText(PRIDEConverter.getProperties().getProteinProphetFileName());
        }

        if (PRIDEConverter.getProperties().getSpectrumFilesFolderName() != null) {
            spectrumFileFolderJTextField.setText(PRIDEConverter.getProperties().getSpectrumFilesFolderName());
        }

        proteinIdFilterJTextField.setText("" + PRIDEConverter.getProperties().getProteinIdentificationFilter());

        peptideProphetConfidenceLevelJSpinner.setValue(
                new Double(PRIDEConverter.getProperties().getPeptideProphetThreshold()));

        proteinProphetConfidenceLevelJSpinner.setValue(
                new Double(PRIDEConverter.getProperties().getProteinProphetThreshold()));
        
        // sets the icon of the frame
        setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().
                getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF")));
        
        setTitle(PRIDEConverter.getWizardName() + " " +
                PRIDEConverter.getPrideConverterVersionNumber() + " - " + getTitle());

        if (location != null) {
            setLocation(location);
        } else {
            setLocationRelativeTo(null);
        }

        // check if the next button should be enabled or not
        mandatoryFieldsCheck();

        fileSelectionJPanel.requestFocus();
        setVisible(true);
    }

    /**
     * Checks if all mandatory information is filled in and enables or disables 
     * the Next button.
     */
    private void mandatoryFieldsCheck() {

        if (peptideProphetFileJTextField.getText().length() > 0 &&
                proteinProphetFileJTextField.getText().length() > 0 && 
                spectrumFileFolderJTextField.getText().length() > 0) {
            nextJButton.setEnabled(true);
        } else {
            nextJButton.setEnabled(false);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        nextJButton = new javax.swing.JButton();
        backJButton = new javax.swing.JButton();
        cancelJButton = new javax.swing.JButton();
        fileSelectionJPanel = new javax.swing.JPanel();
        proteinProphetFileJButton = new javax.swing.JButton();
        proteinProphetFileJTextField = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        peptideProphetFileJTextField = new javax.swing.JTextField();
        findPeptideProphetFileJButton = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        spectrumFileFolderJTextField = new javax.swing.JTextField();
        spectumFileFolderJButton = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        helpJButton = new javax.swing.JButton();
        aboutJButton = new javax.swing.JButton();
        jPanel1 = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        peptideProphetConfidenceLevelJSpinner = new javax.swing.JSpinner();
        proteinProphetConfidenceLevelJSpinner = new javax.swing.JSpinner();
        jLabel2 = new javax.swing.JLabel();
        jPanel2 = new javax.swing.JPanel();
        jLabel6 = new javax.swing.JLabel();
        proteinIdFilterJTextField = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);
        setTitle("File Selection - Step 1 of 8");
        setResizable(false);
        addWindowListener(new java.awt.event.WindowAdapter() {
            public void windowClosing(java.awt.event.WindowEvent evt) {
                formWindowClosing(evt);
            }
        });

        nextJButton.setText("Next  >");
        nextJButton.setEnabled(false);
        nextJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        nextJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextJButtonActionPerformed(evt);
            }
        });

        backJButton.setText("< Back");
        backJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        backJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backJButtonActionPerformed(evt);
            }
        });

        cancelJButton.setText("Cancel");
        cancelJButton.setPreferredSize(new java.awt.Dimension(81, 23));
        cancelJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelJButtonActionPerformed(evt);
            }
        });

        fileSelectionJPanel.setBorder(javax.swing.BorderFactory.createTitledBorder("File Selection"));

        proteinProphetFileJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        proteinProphetFileJButton.setToolTipText("Select File");
        proteinProphetFileJButton.setBorderPainted(false);
        proteinProphetFileJButton.setContentAreaFilled(false);
        proteinProphetFileJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                proteinProphetFileJButtonActionPerformed(evt);
            }
        });

        proteinProphetFileJTextField.setEditable(false);
        proteinProphetFileJTextField.setToolTipText("The prot.xml file");

        jLabel7.setText("ProteinProphet File: ");
        jLabel7.setToolTipText("The prot.xml file");

        jLabel8.setText("PeptideProphet File: ");
        jLabel8.setToolTipText("The pep.xml file");

        peptideProphetFileJTextField.setEditable(false);
        peptideProphetFileJTextField.setToolTipText("The pep.xml file");

        findPeptideProphetFileJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        findPeptideProphetFileJButton.setToolTipText("Select File");
        findPeptideProphetFileJButton.setBorderPainted(false);
        findPeptideProphetFileJButton.setContentAreaFilled(false);
        findPeptideProphetFileJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                findPeptideProphetFileJButtonActionPerformed(evt);
            }
        });

        jLabel10.setText("Spectrum File Folder: ");
        jLabel10.setToolTipText("The folder with the mzXML or MGF files");

        spectrumFileFolderJTextField.setEditable(false);
        spectrumFileFolderJTextField.setToolTipText("The folder with the mzXML or MGF files");

        spectumFileFolderJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/Directory.gif"))); // NOI18N
        spectumFileFolderJButton.setToolTipText("Select Folder");
        spectumFileFolderJButton.setBorderPainted(false);
        spectumFileFolderJButton.setContentAreaFilled(false);
        spectumFileFolderJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                spectumFileFolderJButtonActionPerformed(evt);
            }
        });

        org.jdesktop.layout.GroupLayout fileSelectionJPanelLayout = new org.jdesktop.layout.GroupLayout(fileSelectionJPanel);
        fileSelectionJPanel.setLayout(fileSelectionJPanelLayout);
        fileSelectionJPanelLayout.setHorizontalGroup(
            fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fileSelectionJPanelLayout.createSequentialGroup()
                .addContainerGap()
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel7)
                    .add(jLabel10)
                    .add(jLabel8))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 60, Short.MAX_VALUE)
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, fileSelectionJPanelLayout.createSequentialGroup()
                        .add(peptideProphetFileJTextField)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(findPeptideProphetFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, fileSelectionJPanelLayout.createSequentialGroup()
                        .add(proteinProphetFileJTextField)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(proteinProphetFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, fileSelectionJPanelLayout.createSequentialGroup()
                        .add(spectrumFileFolderJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 397, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(spectumFileFolderJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 27, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        fileSelectionJPanelLayout.setVerticalGroup(
            fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(fileSelectionJPanelLayout.createSequentialGroup()
                .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .addContainerGap()
                        .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(peptideProphetFileJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(findPeptideProphetFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(proteinProphetFileJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(proteinProphetFileJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(18, 18, 18)
                        .add(fileSelectionJPanelLayout.createParallelGroup(org.jdesktop.layout.GroupLayout.CENTER)
                            .add(spectrumFileFolderJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(spectumFileFolderJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(17, 17, 17)
                        .add(jLabel8))
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(65, 65, 65)
                        .add(jLabel7))
                    .add(fileSelectionJPanelLayout.createSequentialGroup()
                        .add(108, 108, 108)
                        .add(jLabel10)))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        fileSelectionJPanelLayout.linkSize(new java.awt.Component[] {findPeptideProphetFileJButton, peptideProphetFileJTextField, proteinProphetFileJButton, proteinProphetFileJTextField, spectrumFileFolderJTextField, spectumFileFolderJButton}, org.jdesktop.layout.GroupLayout.VERTICAL);

        jLabel3.setFont(new java.awt.Font("Tahoma", 2, 11));
        jLabel3.setText("Select the files you want to convert data from, and click on 'Next'  to continue.");

        helpJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/help.GIF"))); // NOI18N
        helpJButton.setToolTipText("Help");
        helpJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpJButtonActionPerformed(evt);
            }
        });

        aboutJButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/no/uib/prideconverter/icons/prideConverter_16.GIF"))); // NOI18N
        aboutJButton.setToolTipText("About");
        aboutJButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                aboutJButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Selection Thresholds"));

        jLabel1.setText("PeptideProphet Threshold:");
        jLabel1.setToolTipText("Probability threshold for acceptance of a peptide identification");

        peptideProphetConfidenceLevelJSpinner.setModel(new javax.swing.SpinnerNumberModel(0.9d, 0.01d, 1.0d, 0.01d));
        peptideProphetConfidenceLevelJSpinner.setToolTipText("Probability threshold for acceptance of a peptide identification");

        proteinProphetConfidenceLevelJSpinner.setModel(new javax.swing.SpinnerNumberModel(0.9d, 0.01d, 1.0d, 0.01d));
        proteinProphetConfidenceLevelJSpinner.setToolTipText("Probability threshold for acceptance of a protein identification");

        jLabel2.setText("ProteinProphet Threshold:");
        jLabel2.setToolTipText("Probability threshold for acceptance of a protein identification");

        org.jdesktop.layout.GroupLayout jPanel1Layout = new org.jdesktop.layout.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel1)
                        .add(37, 37, 37)
                        .add(peptideProphetConfidenceLevelJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jPanel1Layout.createSequentialGroup()
                        .add(jLabel2)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .add(proteinProphetConfidenceLevelJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 54, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(388, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel1Layout.createSequentialGroup()
                .add(19, 19, 19)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel1)
                    .add(peptideProphetConfidenceLevelJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(jPanel1Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel2)
                    .add(proteinProphetConfidenceLevelJSpinner, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(23, Short.MAX_VALUE))
        );

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Protein Identifications"));

        jLabel6.setText("Protein Identification Filter:");
        jLabel6.setToolTipText("Ignore All Protein Identifications Starting With This Tag");

        proteinIdFilterJTextField.setToolTipText("Ignore All Protein Identifications Starting With This Tag");

        org.jdesktop.layout.GroupLayout jPanel2Layout = new org.jdesktop.layout.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jLabel6)
                .add(33, 33, 33)
                .add(proteinIdFilterJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 402, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .add(jPanel2Layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel6)
                    .add(proteinIdFilterJTextField, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(fileSelectionJPanel, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 23, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 25, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 332, Short.MAX_VALUE)
                        .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                        .add(18, 18, 18)
                        .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                    .add(jSeparator1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 655, Short.MAX_VALUE)
                    .add(jLabel3)
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, jPanel1, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .add(jPanel2, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .add(fileSelectionJPanel, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jPanel2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 143, Short.MAX_VALUE)
                .add(jLabel3)
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(jSeparator1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 10, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(7, 7, 7)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(cancelJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(nextJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(backJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(helpJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(aboutJButton, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 24, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents
    /**
     * Closes the frame and the wizard.
     * 
     * @param evt
     */
    private void cancelJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelJButtonActionPerformed
        this.setVisible(false);
        this.dispose();
        PRIDEConverter.cancelConvertion();
    }//GEN-LAST:event_cancelJButtonActionPerformed

    /**
     * Closes the frame and opens the next frame (SpectraSelection).
     * 
     * @param evt
     */
    private void nextJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextJButtonActionPerformed

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        boolean saveOk = true;

        try {
            PRIDEConverter.getProperties().setPeptideProphetThreshold(
                    (Double) peptideProphetConfidenceLevelJSpinner.getValue());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, 
                    "PeptideProphet threshold is not a number!", "PeptideProphet Threshold",
                    JOptionPane.INFORMATION_MESSAGE);
            peptideProphetConfidenceLevelJSpinner.requestFocus();
            saveOk = false;
        }

        try {
            PRIDEConverter.getProperties().setProteinProphetThreshold(
                    (Double) proteinProphetConfidenceLevelJSpinner.getValue());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, 
                    "ProteinProphet threshold is not a number!", "ProteinProphet Threshold",
                    JOptionPane.INFORMATION_MESSAGE);
            proteinProphetConfidenceLevelJSpinner.requestFocus();
            saveOk = false;
        }

        PRIDEConverter.getProperties().setProteinIdentificationFilter(proteinIdFilterJTextField.getText());

        if (saveOk) {
            //new SpectraSelectionWithIdentifications(prideConverter, this.getLocation());
            //new SpectraSelectionNoIdentifications(prideConverter, this.getLocation());
            new ExperimentProperties(this.getLocation());

            this.setVisible(false);
            this.dispose();
        }

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_nextJButtonActionPerformed

    /**
     * @see #cancelJButtonActionPerformed(java.awt.event.ActionEvent)
     */
    private void formWindowClosing(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowClosing
        cancelJButtonActionPerformed(null);
    }//GEN-LAST:event_formWindowClosing

    /**
     * Opens a Help dialog.
     * 
     * @param evt
     */
    private void helpJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/DataFileSelectionTPP.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_helpJButtonActionPerformed

    /**
     * Opens an About PRIDE Converter dialog.
     * 
     * @param evt
     */
    private void aboutJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_aboutJButtonActionPerformed
        setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));
        new HelpDialog(this, false, getClass().getResource("/no/uib/prideconverter/helpfiles/AboutPRIDE_Converter.html"));
        setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_aboutJButtonActionPerformed

    /**
     * Closes the frame and opens the previous frame (DataSourceSelection).
     * 
     * @param evt
     */
    private void backJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backJButtonActionPerformed

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        boolean saveOk = true;

        try {
            PRIDEConverter.getProperties().setPeptideProphetThreshold(
                    (Double) peptideProphetConfidenceLevelJSpinner.getValue());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, 
                    "PeptideProphet threshold is not a number!", "PeptideProphet Threshold",
                    JOptionPane.INFORMATION_MESSAGE);
            peptideProphetConfidenceLevelJSpinner.requestFocus();
            saveOk = false;
        }

        try {
            PRIDEConverter.getProperties().setProteinProphetThreshold(
                    (Double) proteinProphetConfidenceLevelJSpinner.getValue());
        } catch (NumberFormatException e) {
            JOptionPane.showMessageDialog(this, 
                    "ProteinProphet threshold is not a number!", "ProteinProphet Threshold",
                    JOptionPane.INFORMATION_MESSAGE);
            proteinProphetConfidenceLevelJSpinner.requestFocus();
            saveOk = false;
        }

        PRIDEConverter.getProperties().setProteinIdentificationFilter(proteinIdFilterJTextField.getText());

        if (saveOk) {

            new DataSourceSelection(this.getLocation());
            this.setVisible(false);
            this.dispose();
        }

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
    }//GEN-LAST:event_backJButtonActionPerformed

    /**
     * Opens a filechooser where the ProteinProphet file can be selected.
     * 
     * @param evt
     */
    private void proteinProphetFileJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_proteinProphetFileJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (proteinProphetFileJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(proteinProphetFileJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle("Select the ProteinProphet File");
        chooser.setFileFilter(new ProtXmlFileFilter());

        String path;

        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            proteinProphetFileJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setProteinProphetFileName(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList<String>());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_proteinProphetFileJButtonActionPerformed

    /**
     * Opens a filechooser where the PeptideProphet file can be selected.
     * 
     * @param evt
     */
    private void findPeptideProphetFileJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_findPeptideProphetFileJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (peptideProphetFileJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(peptideProphetFileJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
        chooser.setDialogTitle("Select the PeptideProphet File");
        chooser.setFileFilter(new PepXmlFileFilter());

        String path;

        int returnVal = chooser.showOpenDialog(this);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            peptideProphetFileJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setPeptideProphetFileName(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_findPeptideProphetFileJButtonActionPerformed

    /**
     * Opens a filechooser where the mzXML or mgf files folder can be selected.
     * 
     * @param evt
     */
    private void spectumFileFolderJButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_spectumFileFolderJButtonActionPerformed
        this.setCursor(new java.awt.Cursor(java.awt.Cursor.WAIT_CURSOR));

        JFileChooser chooser = new JFileChooser(
                PRIDEConverter.getUserProperties().getCurrentSourceFileLocation());

        if (spectrumFileFolderJTextField.getText().length() > 0) {
            chooser.setSelectedFile(new File(spectrumFileFolderJTextField.getText()));
        }

        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setDialogTitle("Select the Spectrum Files Folder");

        String path;

        int returnVal = chooser.showOpenDialog(this);
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            path = (chooser.getSelectedFile().getAbsoluteFile().getPath());
            spectrumFileFolderJTextField.setText(path);
            PRIDEConverter.getUserProperties().setSourceFileLocation(chooser.getSelectedFile().getPath());
            PRIDEConverter.getProperties().setSpectrumFilesFolderName(path);

            PRIDEConverter.getProperties().setSpectrumTableModel(null);
            PRIDEConverter.getProperties().setInstrumentDetailsExtracted(false);
            PRIDEConverter.getProperties().setSelectedSourceFiles(new ArrayList());
            PRIDEConverter.getProperties().setSpectraSelectionCriteria(null);
            PRIDEConverter.getProperties().setDataFileHasBeenLoaded(false);
            PRIDEConverter.getProperties().setSampleDetailsExtracted(false);
            PRIDEConverter.getProperties().setContactInfoExtracted(false);
            PRIDEConverter.getProperties().setCurrentQuantificationSelection(new ArrayList());
            PRIDEConverter.getProperties().setSpectrumCvParams(new HashMap<String, ArrayList<CvParam>>());
            PRIDEConverter.getProperties().setSpectrumUserParams(new HashMap<String, ArrayList<UserParam>>());
        }

        mandatoryFieldsCheck();

        this.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));
}//GEN-LAST:event_spectumFileFolderJButtonActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton aboutJButton;
    private javax.swing.JButton backJButton;
    private javax.swing.JButton cancelJButton;
    private javax.swing.JPanel fileSelectionJPanel;
    private javax.swing.JButton findPeptideProphetFileJButton;
    private javax.swing.JButton helpJButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JButton nextJButton;
    private javax.swing.JSpinner peptideProphetConfidenceLevelJSpinner;
    private javax.swing.JTextField peptideProphetFileJTextField;
    private javax.swing.JTextField proteinIdFilterJTextField;
    private javax.swing.JSpinner proteinProphetConfidenceLevelJSpinner;
    private javax.swing.JButton proteinProphetFileJButton;
    private javax.swing.JTextField proteinProphetFileJTextField;
    private javax.swing.JTextField spectrumFileFolderJTextField;
    private javax.swing.JButton spectumFileFolderJButton;
    // End of variables declaration//GEN-END:variables
}
